#include <wx/msgdlg.h>
#include "HeliCtrlPanel.h"
#include "hal.h"
#include "protocol_parser.h"

BEGIN_EVENT_TABLE(HeliCtrlPanel, MainPanel)
	//EVT_SOFT_JOY_MOVE(wxID_ANY,HeliCtrlPanel::OnSoftJoy)
	//EVT_SOFT_JOY_BUTTON(wxID_ANY,HeliCtrlPanel::OnSoftJoy)
	EVT_TIMER(TELEMETRY_TIMER_ID, HeliCtrlPanel::OnTelemetryTimer)
END_EVENT_TABLE()

HeliCtrlPanel::HeliCtrlPanel( wxWindow* parent )
: MainPanel( parent ), m_TelemetryTimer(this,TELEMETRY_TIMER_ID)
{
	m_SoftJoy->Connect( wxEVT_SOFT_JOY_MOVE, wxJoystickEventHandler( HeliCtrlPanel::OnSoftJoy ), NULL, this );
	m_SoftJoy->Connect( wxEVT_SOFT_JOY_BUTTON, wxJoystickEventHandler( HeliCtrlPanel::OnSoftJoy ), NULL, this );

	com_port->Select(7);
}

HeliCtrlPanel::~HeliCtrlPanel()
{
}

wxVertex2D HeliCtrlPanel::PrescaleToPercentx10(wxPoint &point)
{
	wxVertex2D scaled;

	unsigned short x,y;
	x = (unsigned short)point.x;
	y = (unsigned short)point.y;

	scaled.x = (int) (((float)x/0xffff) *1000); 
	scaled.y = (int) (((float)y/0xffff) *1000); 

	return scaled;
}

void HeliCtrlPanel::OnSoftJoy( wxJoystickEvent& event )
{
	if(event.GetEventType() == wxEVT_SOFT_JOY_MOVE)
	{
		//update machine
		double yawCor = (double)yaw_correction->GetValue();
		wxVertex2D scaled;
		wxPoint diff;
		diff = m_PrevJoyPos - event.GetPosition();

		if( abs(diff.x) < 10 && abs(diff.y) < 10)
			return;

		m_PrevJoyPos = event.GetPosition();

		scaled=PrescaleToPercentx10(event.GetPosition());
		double pes = scaled.y/320;
		
		yawCor = yawCor - pes;

		double v1,v2;
		v1 = (1000-scaled.x)/(yawCor);		
		v2 = scaled.y/10;

		if(v1 > 100)
			v1 = 100;
		if(v1 < 50)
			v1 = 50;
		
		SetHelicopterThrust(v1,v2);

		wxPoint pos = event.GetPosition();
		wxLogDebug(_("Pos: [%d,%d]"),pos.x,pos.y);
	}

	if(event.GetEventType() == wxEVT_SOFT_JOY_BUTTON)
	{
		/* 4  */ if(event.GetButtonChange()&0x8) OnJoystickButtonEvent(4,event.GetButtonState()&0x8?1:0);
		/* 2  */ if(event.GetButtonChange()&0x2) OnJoystickButtonEvent(2,event.GetButtonState()&0x2?1:0);
		/* 3  */ if(event.GetButtonChange()&0x4) OnJoystickButtonEvent(3,event.GetButtonState()&0x4?1:0);
		/*shot*/ if(event.GetButtonChange()&0x1) OnJoystickButtonEvent(1,event.GetButtonState()&0x1?1:0);
		//wxLogDebug(_("Button: [%d]"),evt.ButtonIsDown(wxJOY_BUTTON1));
	}
}

void HeliCtrlPanel::OnJoystickButtonEvent(int Button, bool Down)
{
	if(Button==1) EnablePowerForEngines(Down);
}

void HeliCtrlPanel::OnOK( wxCommandEvent& event )
{
	wxMessageBox( wxT("OK Button Pressed") );
}

void HeliCtrlPanel::PortOpen_ButtonClick( wxCommandEvent& event )
{ 
	if(m_PortOpen->GetLabel() == _T("Open"))
	{
	HalInit(com_port->GetStringSelection().wchar_str());
	m_PortOpen->SetLabel(_T("Close"));
	m_PortOpen->SetBackgroundColour(*wxGREEN);
	m_TelemetryTimer.Start(2000);
	mActivityPanel->SetBackgroundColour(wxColor(_("YELLOW")));
	//m_SoftJoy->SetCursorColor(*wxGREEN);

	}
	else
	{
	HalDeInit();
	m_PortOpen->SetLabel(_T("Open"));
	m_PortOpen->SetBackgroundColour(*wxRED);
	m_TelemetryTimer.Stop();
	mActivityPanel->SetBackgroundColour(*wxRED);
	//m_SoftJoy->SetCursorColor(*wxBLACK);
	}
	mActivityPanel->Refresh();

}

void HeliCtrlPanel::OnTelemetryTimer(wxTimerEvent& event)
{
	double Battery_Voltage;
	DWORD activtick;
	Battery_Voltage = GetHelicopterAnalogMeasure(BATTERY_VOLTAGE);	
	battery_voltage->SetLabel(wxString::Format(wxT("Battery: %2.2f [V]"),GetHelicopterAnalogMeasure(BATTERY_VOLTAGE)));
	activtick = GetLastConnectionSignalingTime();


	wxColor bg;
	DWORD time = GetTickCount() - activtick;
	if(time > 11000)//10s
		bg = wxColor(_("YELLOW"));
	else if(time > 14000)
		bg = *wxRED;
	else
		bg = *wxGREEN;

	if(bg!=mActivityPanel->GetBackgroundColour())
	{
		mActivityPanel->SetBackgroundColour(bg);
		mActivityPanel->Refresh();
	}
}

void HeliCtrlPanel::OnBeepBtn( wxCommandEvent& event )
{	
	if(beep_btn->GetValue())
		StartBeep((unsigned char)beep_type->GetValue());
	else
		StopBeep();
}
